import java.io.File;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;
import java.util.Scanner;

class NameException extends RuntimeException{
    public NameException(String message){
        super(message);
    }


}
class PasswordException extends RuntimeException{
    public PasswordException(String message){
        super(message);
    }

}

public class Test {
    private static final String userName = "author";
    private static final String password = "12138";

    public static void login(String userName, String password) throws NameException,PasswordException{
        if(!Test.userName.equals(userName)){// 用户名不相同，进入if语句
            throw new NameException("用户名输入错误！");
        }
        if (!Test.password.equals(password)){
            throw new PasswordException("password输入错误！");
        }
    }

    public static void main(String[] args) {
        try{
            login("author","1213");
        }catch(NameException e){
            e.printStackTrace();
            System.out.println("发现一个用户名不匹配异常");
        }catch(PasswordException e){
            e.printStackTrace();
            System.out.println("发现一个密码不匹配异常");
        }finally{
            System.out.println("finally 执行了");
        }

    }



    public static void main9(String[] args)  {
        System.out.println(readFile());
    }
    public static String readFile() {
        // 尝试打开文件, 并读其中的一行.
        File file = new File("d:/test.txt");
        // 使用文件对象构造 Scanner 对象.
        Scanner sc = null;
        try {
            sc = new Scanner(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return sc.nextLine();
    }



    public static void func2(int x)throws ArithmeticException{
        if(x==0){
            throw new ArithmeticException("/ by 0");
        }
        System.out.println(10/x);
    }
    public static void main8(String[] args) {
          try {
              func2(0);
          }catch (ArithmeticException e){
              e.printStackTrace();
              System.out.println("捕获到一个算术异常");
          }
    }


    public static int func(){
        int a =10;
        try {
            return a;
        }catch (ArithmeticException e){// 随便写一个catch捕获异常，因为这里放生不了异常。
            e.printStackTrace();
        }finally {
            return 20;
        }
    }

    public static void main7(String[] args) {
        System.out.println(func());
    }

    public static void fucn(int n)throws ArithmeticException{
        System.out.println(10/n);
    }
    public static void main6(String[] args) {
        try{
            fucn(0);
        }catch (ArithmeticException e){
            e.printStackTrace();
            System.out.println("这里捕获到一个算术异常");
        }
        System.out.println("hhh");
    }


    public static void main5(String[] args) {
        int[] array = {1,2,3};
        try {
//            array = null;
            System.out.println(array[5]);
            System.out.println("haaaa");
        }catch (ArrayIndexOutOfBoundsException e){
            e.printStackTrace();
            System.out.println("捕获到一个数组越界异常");
        }catch (IndexOutOfBoundsException e){
            e.printStackTrace();
            System.out.println("捕获到一个越界异常");
        }
    }

    public static void main4(String[] args) {
        int[] array = {1,2,3};
        try {
            array = null;
            System.out.println(array[5]);
            System.out.println("haaaa");
        }catch (NullPointerException | ArrayIndexOutOfBoundsException e){
            e.printStackTrace();
            System.out.println("捕获到一个空指针异常/数组越界异常");
        }
    }

    public static void main3(String[] args) {
        int[] array = {1,2,3};
        try {
            array = null;
            System.out.println(array[5]);
            System.out.println("haaaa");
        }catch (NullPointerException e){
            e.printStackTrace();
            System.out.println("捕获到一个空指针异常");
        }catch (ArrayIndexOutOfBoundsException e){
            e.printStackTrace();
            System.out.println("捕获到一个数组越界异常");
        }
    }

    public static void main2(String[] args) {
        int[] array = {1,2,3};
        try {
            System.out.println(array[5]);
            System.out.println("hahhahahah");
        }catch (ArrayIndexOutOfBoundsException e){
            e.printStackTrace();
            System.out.println("发生数组越界异常");
        }

        System.out.println("hei");
    }

    public static void main1(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {
            int i = sc.nextInt();// 输入必须为数字
            System.out.println(10 / i);// i 不能为 0
        } catch (InputMismatchException e) {
            e.printStackTrace();
            System.out.println("捕获到一个输入数据不匹配异常");
        } catch (ArithmeticException e) {
            e.printStackTrace();
            System.out.println("捕获到一个算术异常");
        } finally {
            // 一般用作资源的关闭。这是一种规则
            System.out.println("finally 在这里被执行了");
        }
    }
}


//class  Person implements Cloneable{
//    int a ;
//    @Override
//    protected Object clone() throws CloneNotSupportedException {
//        return super.clone();
//    }
//}
//
//public class Test {
//    public static void main(String[] args){
//        Person person = new Person();
//        try {
//            Person person1 = (Person) person.clone();
//        }catch (CloneNotSupportedException e){
//            e.printStackTrace();
//            System.out.println("捕获一个不支持克隆异常");
//        }
//
//    }
//}

        // 算术异常
//        System.out.println(10 / 0);

        // 空指针异常
//        String str = null;
//        System.out.println(str.length());

        // 数组 索引/下标 越界异常
//        int[] array = new int[3];
//        System.out.println(array[5]);
//    }
//}
